package com.yff.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yff.dao.IMessageDao;
import com.yff.dao.impl.MessageDaoImpl;
import com.yff.model.Message;
import com.yff.model.User;

/**
 * 负责完成用户信息的CRUD、登录、注销等功能
 * @author Administrator
 *
 */

@WebServlet("/message")
public class MessageServlet extends HttpServlet {

	private static final long serialVersionUID = -3704200856467333570L;
	
	private IMessageDao messageDao=new MessageDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");		
		PrintWriter out=response.getWriter();
		HttpSession session = request.getSession();
		User loginUser=(User) session.getAttribute("loginInfo");
		
		System.out.println("user="+loginUser);
		
		
		
		String type=request.getParameter("type");
		
		String idStr=request.getParameter("id");
		int id;
		try {
			id=Integer.valueOf(idStr);
		} catch (Exception e) {
			id=0;
		}
		
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		
		
		Boolean flag=false;
		
		int ret=0;
		
		if(type.equals("add")) {
			add(request, response, loginUser, title, content);
		}
		if(type.equals("del")) {
			del(request, response);
		}
	    if(type.equals("get")) {
	    	//去数据库中获取id对应的message
	    	Message m=messageDao.get(id);
	    	request.setAttribute("message", m);
	    	RequestDispatcher dispatcher=request.getRequestDispatcher("messageedit.jsp");
			dispatcher.forward(request,response);
	    }
	    if(type.equals("edit")) {
	    	Message m=new Message(id,title,content);
	    	ret=messageDao.editTileAndContentById(m);
	    	if(ret!=0) {
				response.sendRedirect(request.getContextPath()+"/index");
			}
	    }
	}
	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int ret;
		String id=request.getParameter("id");
		ret=messageDao.delete(Integer.parseInt(id));
		if(ret!=0) {
			response.sendRedirect(request.getContextPath()+"/index");
		}
	}
	private void add(HttpServletRequest request, HttpServletResponse response, User loginUser, String title,
			String content) throws IOException {
		int ret;
		Message message=new Message();
		message.setTitle(title);
		message.setContent(content);
		message.setCreateDateTime(new Date());
		message.setUserId(loginUser.getId());
		ret=messageDao.add(message);
		if(ret!=0) {
			response.sendRedirect(request.getContextPath()+"/index");
		}
	}	
	@Override
	protected void doPost(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(requset, response);
	}
	

}
